<template>
	<view :style="{height:PhoneInfo.windowHeight + 'px'}">
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<view class="u-search-box  cssBox">
			<view class="u-search-inner">
				<u-search v-model="searchKeyword" @search="this.getuserequipmententrust()" shape="round"
					:clearabled="true" :show-action="false" input-align="left">
				</u-search>
			</view>
		</view>
		<view class="uniview" style="margin-bottom: 0px;">
			<uni-card>
				<view class="title">
					<view class="text" @click="allchecked">
						<checkbox :checked="allcheckbox" style="transform:scale(0.7)" />
						<text style="margin-left: 5px;">全部选中</text>
					</view>
					<view class="switch">
						<selectSwitch :switchList='switchList' :defaultSwitch="true" :checked_bj_color="checkedcolor"
							@change="changeallswitch" />
					</view>
				</view>
			</uni-card>
		</view>
		<view style="height: 65%; overflow: hidden;">
			<scroll-view scroll-y="true" style="height: 100%;" @scrolltolower="scrolltolower">
				<view class="uniview" v-for="(item,index) in equipmentList" :key="index">
					<uni-card>
						<template v-slot:title>
							<view class="title">
								<view class="text" @click="checked(item.equipmentRealId)">
									<checkbox :checked="item.assigned" style="transform:scale(0.7)" />
									<text style="margin-left: 5px;">{{item.equipmentName}}</text>
								</view>
								<view class="switch">
									<selectSwitch :switchList='switchList'
										:defaultSwitch="item.assignStatus==1?true:false"
										:checked_bj_color="item.assignStatus==1?'#18bc37':'#3c9cff'"
										@change="changeswitch(item.equipmentRealId)" />
								</view>
							</view>
						</template>
					</uni-card>
				</view>
				<u-loadmore :status="loadStatus" :loading-text="loadingText" :loadmore-text="loadmoreText"
					nomore-text=' ' bgColor="#efeff4"></u-loadmore>
			</scroll-view>
		</view>
		<view style="margin: auto; width: 90%;">
			<u-button type="primary" text="确定" @click="alter"></u-button>
		</view>
	</view>
</template>

<script>
	import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
	export default {
		components: {
			selectSwitch
		},
		data() {
			return {
				PhoneInfo: '',
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					isCustomFn: true,
					btnType: "type2",
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				switchList: ['查看', '管理'],
				allcheckbox: false,
				checkedcolor: '#18bc37',
				loadStatus: 'loadmore',
				loadingText: '加载中...',
				loadmoreText: '上拉加载更多',
				userinfo: '',
				pageIndex: '1',
				pageSize: '15',
				searchKeyword: "",
				count: '',
				equipmentList: [],
				userid: '',
			}
		},
		onLoad(parment) {
			this.PhoneInfo = this.$utils.GetPhoneInfo();
			this.userinfo = uni.getStorageSync('userinfo');
			this.userid = parment.userid;
			this.mysNavConfig.navTitle.text = parment.name;
			this.getuserequipmententrust();
		},
		methods: {
			//获取指定用户的授权设备
			getuserequipmententrust() {
				uni.showLoading({
					title: "加载中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_lookentrustequip.replace(
						"{userRealId}", this.userinfo.userRealId).replace("{assignedUserRealId}", this.userid),
					data: {
						page: this.pageIndex,
						limit: this.pageSize,
						equipmentName: this.searchKeyword,
					},
					dataType: "json",
					method: "GET",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.count = res.data.count
							this.equipmentList = res.data.data
							if (this.count < this.pageSize) {
								this.loadStatus = "nomore";
							} else { //否则就是还有数据
								this.loadStatus = "loadmore";
							}
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "加载失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
			//返回
			onLeftClick() {
				uni.redirectTo({
					url: '/pages/entrust/equipmentEntrust'
				})
			},
			//全部选中
			allchecked() {
				if (this.allcheckbox) {
					this.allcheckbox = false;
					for (let it of this.equipmentList) {
						it.assigned = false;
					}
				} else {
					this.allcheckbox = true;
					for (let it of this.equipmentList) {
						it.assigned = true;
					}
				}
			},
			//选中
			checked(value) {
				for (let it of this.equipmentList) {
					if (value == it.equipmentRealId) {
						if (it.assigned) {
							it.assigned = false
						} else {
							it.assigned = true
						}
					}
				}
			},
			//全选改变switch
			changeallswitch(e) {
				if (e) {
					this.checkedcolor = '#18bc37'
					for (let itemr of this.equipmentList) {
						itemr.assignStatus = 1
					}
				} else {
					this.checkedcolor = '#3c9cff'
					for (let itemr of this.equipmentList) {
						itemr.assignStatus = 2
					}
				}
			},
			//改变switch
			changeswitch(value) {
				for (let it of this.equipmentList) {
					if (value == it.equipmentRealId) {
						if (it.assignStatus == 1) {
							it.assignStatus = 2
						} else {
							it.assignStatus = 1
						}
					}
				}
			},
			//修改
			alter() {
				uni.showLoading({
					title: "修改中...",
					mask: true,
				});
				var array = []
				for (let vale of this.equipmentList) {
					if (vale.assigned == true) {
						array.push({
							equipmentRealId: vale.equipmentRealId,
							assignStatus: vale.assignStatus,
						})
					}
				}
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_edituserentrustequip.replace(
						"{userRealId}",
						this.userinfo.userRealId).replace(
						"{assignedUserRealId}",
						this.userid),
					data: array,
					dataType: "json",
					method: "POST",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.$utils.showToast(this, "修改成功！", 'success', 2,
								'/pages/entrust/equipmentEntrust');
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "修改失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
			//加载
			scrolltolower() {
				if (this.count < this.pageSize) {
					this.loadStatus = "nomore";
				} else {
					setTimeout(() => {
						this.loadStatus = "loading";
						this.pageSize += 15;
						this.getuserequipmententrust();
					}, 500);
				}
			},
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.pageSize = 15;
			this.getuserequipmententrust();
			uni.stopPullDownRefresh();
		}
	};
</script>

<style lang="scss">
	.cssBox {
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;
		margin-bottom: 15rpx;
	}

	.uniview {
		margin: 20px;
		padding: 0px 10px;
		background-color: #ffffff;
		box-shadow: 0px 0px 3px 1px rgba(212, 214, 220, 1.0);
		border-radius: 3%;
	}

	.text {
		width: 73%;
		display: inline-block;
	}

	.switch {
		width: 27%;
		display: inline-block;
	}

	.title {
		padding: 0px 10px;
		line-height: 46px;
		height: 46px;
		border-bottom: 0.5px solid #e0e1e2;
	}
</style>